package org.cycuchy.hp.app {
	import com.adobe.net.DynamicURLLoader;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	
	import mx.core.Application;
	
	import org.cycuchy.hp.data.EmPeCzysData;
	import org.cycuchy.hp.data.FotosData;
	import org.cycuchy.hp.data.KapstelkiData;
	import org.cycuchy.hp.event.XMLParsedEvent;
	import org.cycuchy.hp.service.CHService;
	import org.cycuchy.hp.service.ServiceHelper;
	
	
	public final class HPApplicationManager {
		
		private static var _instance:HPApplicationManager;
		
//		private static const XML_FOTOS:String = "assets/xml/fotos.xml?rand=";
		private static const XML_FOTOS:String = "assets/xml/fotos_local.xml?rand=";
		
		public static function get instance():HPApplicationManager {
			if (!HPApplicationManager._instance) {
				HPApplicationManager._instance = new HPApplicationManager();
			}
			return HPApplicationManager._instance;
		}
		
		public function applProgressHandler(event:ProgressEvent):void {
			HPPreloader.instance.applProgressHandler();
		}
		
		public function applInitCompleteHandler(event:Event):void {
			HPPreloader.instance.applInitCompleteHandler();
			ServiceHelper.invokeMethod(new CHService(), empeczysXmlComplete, empeczysXmlProgress, empeczysXmlIOError, "assets/xml/empeczys.xml?rand=" + Math.random());			
		}
		
		private function empeczysXmlComplete(event:Event):void {
			parseAndDispatch(DynamicURLLoader(event.target).data, EmPeCzysData.xml2EmPeCzysData, XMLParsedEvent.EMPECZY_DONE);			
			ServiceHelper.invokeMethod(new CHService(), kapstelkiXmlComplete, kapstelkiXmlProgress, kapstelkiXmlIOError, "assets/xml/kapstelki.xml?rand=" + Math.random());			
		}
		
		private function empeczysXmlProgress(event:ProgressEvent):void {
			HPPreloader.instance.xmlProgressHandler();
		}
		
		private function empeczysXmlIOError(event:IOErrorEvent):void {
			Application.application.dispatchEvent(new XMLParsedEvent(XMLParsedEvent.EMPECZY_ERROR));
			ServiceHelper.invokeMethod(new CHService(), kapstelkiXmlComplete, kapstelkiXmlProgress, kapstelkiXmlIOError, "assets/xml/kapstelki.xml?rand=" + Math.random());			
		}
		
		private function kapstelkiXmlComplete(event:Event):void {
//			trace("!complete!");
			parseAndDispatch(DynamicURLLoader(event.target).data, KapstelkiData.xml2KapstelkiData, XMLParsedEvent.KAPSTELIKI_DONE);
			ServiceHelper.invokeMethod(new CHService(), fotosXmlComplete, fotosXmlProgress, fotosXmlIOError, XML_FOTOS + Math.random());			
		}
		
		private function kapstelkiXmlProgress(event:ProgressEvent):void {
//			trace("!progress!: " + event);
			HPPreloader.instance.xmlProgressHandler();
		}
		
		private function kapstelkiXmlIOError(event:IOErrorEvent):void {
//			trace("!ioError!: " + event);
			Application.application.dispatchEvent(new XMLParsedEvent(XMLParsedEvent.KAPSTELKI_ERROR));
			ServiceHelper.invokeMethod(new CHService(), fotosXmlComplete, fotosXmlProgress, fotosXmlIOError, XML_FOTOS + Math.random());			
		}

		private function fotosXmlComplete(event:Event):void {
//			trace("!complete!");
			parseAndDispatch(DynamicURLLoader(event.target).data, FotosData.xml2KapstelkiData, XMLParsedEvent.FOTOS_DONE);
			HPPreloader.instance.timerAllDone();									
		}
		
		private function fotosXmlProgress(event:ProgressEvent):void {
//			trace("!progress!: " + event);
			HPPreloader.instance.xmlProgressHandler();
		}
		
		private function fotosXmlIOError(event:IOErrorEvent):void {
//			trace("!ioError!: " + event);
			Application.application.dispatchEvent(new XMLParsedEvent(XMLParsedEvent.FOTOS_ERROR));
			HPPreloader.instance.timerAllDone();
		}
		
		private function parseAndDispatch(data:String, parseFunction:Function, eventType:String):void {
//			trace("HPApplicationManager::parseAndDispatch data: ", data);
			var xml:XML = new XML(data);
//			trace("HPApplicationManager::parseAndDispatch xml: ", xml.toString());
			var o:Object = parseFunction(xml);
			Application.application.dispatchEvent(new XMLParsedEvent(eventType, o));
		}
		
		
	}
	
}